package com.wen.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wen.mapper.PayDetailsMapper;

import com.wen.mapper.StockDetailsMapper;
import com.wen.pojo.PayDetails;
import com.wen.pojo.StockDetails;
import com.wen.pojo.dto.PayDetailsDto;

import com.wen.service.PayDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class PayDetailsServiceImpl  extends ServiceImpl<PayDetailsMapper, PayDetails> implements PayDetailsService {
   @Autowired
   private PayDetailsMapper mapper;
    @Autowired
    private StockDetailsMapper mappers;
    @Override
    public List<PayDetailsDto> payDetailsById(String id) {
        List<PayDetailsDto> payDetailsDto = mapper.payDetailsById(id);
        return payDetailsDto;
    }

    @Override
    public List<PayDetailsDto> payById(String id) {
        List<PayDetailsDto> payDetailsDtos = mapper.payById(id);
        for (PayDetailsDto p:payDetailsDtos) {
            Long stockId = p.getStockId();
            long productId = p.getProductId();
            long supplierId = p.getSupplierId();
            //根据三个id查这个商品
            StockDetails stockDetails = mappers.stockId(stockId, productId, supplierId);
            //商品不为null就在修改数量然后改状态
            if (stockDetails!=null){
                long ids = stockDetails.getId();
                long amount = p.getAmount();
                boolean flag = mapper.UpStockId(amount, ids);
                if(flag==true){
                    boolean flags = mapper.UpPay(id);
                    if (flags){
                        System.out.println("出库成功");
                    }else {
                        System.out.println("出库失败");
                    }
                }else {
                    System.out.println("修改出库数量失败");
                    break;
                }
            }else {
                System.out.println("出库失败,没有这个商品");
            }
        }
        return payDetailsDtos;
    }
}
